﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmlingyongjilu : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmlingyongjilu(bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void frmlingyongjilu_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btnTra.Enabled = false;
                btnAddnew2.Enabled = false;
                btn_add.Enabled = false;
                btnAddnew3.Enabled = false;
            }
            if (!b量具Search)
            {
                btnSearch領用.Enabled = false;
            }
            if (!b量具Edit)
            {
                btn_edit.Enabled = false;
                btnEdit2.Enabled = false;
                btnEdit3.Enabled = false;
            }
            if (!b量具Del)
            {
                btn_del.Enabled = false;
                btnDelete2.Enabled = false;
                btnDelete3.Enabled = false;
            }
            ResetText領用部門();
            ResetText領用_受理人();
            領用紀錄load();
            領用部門load();
            領用人load();
            領用_受理人load();
        }

        public void ResetTextLingyongjilu()
        {
            //Ck日期1.Checked = false;
            //Ck日期2.Checked = false;
            dateTimePicker_from_歸還日期.ResetText();
            dateTimePicker_From_領用日期.ResetText();
            //dateTimePicker_to_歸還日期.ResetText();
            //dateTimePicker_to_領用日期.ResetText();
            txt受理人.ResetText();
            txt量具名稱.ResetText();
            txt量具編號.ResetText();
            txt領用部門.ResetText();
        }

        public void ResetText領用部門()
        {
            txt領用部門代號.ResetText();
            txt領用部門名稱.ResetText();
        }

        public void ResetText領用人()
        {
            txt領用人代號.ResetText();
            txt領用部門代號2.ResetText();
            txt領用人姓名.ResetText();
        }

        public void ResetText領用_受理人()
        {
            txt受理人代號.ResetText();
            txt受理人姓名.ResetText();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetTextLingyongjilu();
            領用紀錄load();
        }

        void 領用紀錄load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string sp_load = "sp_領用紀錄_load";
            SqlDataAdapter da = new SqlDataAdapter(sp_load, con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            //dataGV領用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGV領用.DataSource = dt;
            PublicClass.setRowNumber(dataGV領用);
            dataGV領用.Columns[0].Width = 0;
            ResetTextLingyongjilu();
            con.Close();
        }

        void 領用部門load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string sp_load = "sp_領用部門_load";
            SqlDataAdapter da = new SqlDataAdapter(sp_load, con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            Dav領用部門.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            Dav領用部門.DataSource = dt;
            PublicClass.setRowNumber(Dav領用部門);
            ResetText領用部門();
            con.Close();
        }

        void 領用人load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string sp_load = "sp_領用人load";
            SqlDataAdapter da = new SqlDataAdapter(sp_load, con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGV領用人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGV領用人.DataSource = dt;
            PublicClass.setRowNumber(dataGV領用人);
            ResetText領用人();
            con.Close();
        }

        void 領用_受理人load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string sp_load = "sp_領用_受理人load";
            SqlDataAdapter da = new SqlDataAdapter(sp_load, con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGV領用_受理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGV領用_受理人.DataSource = dt;
            PublicClass.setRowNumber(dataGV領用_受理人);
            ResetText領用_受理人();
            con.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用部門代號.Text == "")
                    {
                        MessageBox.Show("領用部門代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt領用部門代號.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_領用部門_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@領用部門代號", txt領用部門代號.Text);
                    cmd.Parameters.AddWithValue("@領用部門名稱", txt領用部門名稱.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    領用部門load();
                    ResetText領用部門();
                    con.Close();
                    //reset all text
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用部門load();
                    return;
                }
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用部門代號.Text == "") { MessageBox.Show("請您輸入領用部門代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 領用部門 WHERE 領用部門代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_領用部門_修改";
                            SqlCommand cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            //con.Open();
                            cmd2.Parameters.AddWithValue("@領用部門代號", str);
                            cmd2.Parameters.AddWithValue("@領用部門代號2", txt領用部門代號.Text);
                            cmd2.Parameters.AddWithValue("@領用部門名稱", txt領用部門名稱.Text);
                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            領用部門load();
                            con.Close();
                            ResetText領用部門();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt領用部門代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用部門load();
                    return;
                }
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用部門代號.Text == "") { MessageBox.Show("請您輸入領用部門代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入領用部門代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_領用部門_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@領用部門代號", txt領用部門代號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            領用部門load();
                            con.Close();
                            ResetText領用部門();
                            return;
                        }
                        else
                        {
                            領用部門load();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    領用部門load();
                    return;
                }
            }
        }

        private void Dav領用部門_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(Dav領用部門);
        }

        private void Dav領用部門_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try{
                int r = e.RowIndex;
                str = Dav領用部門.Rows[r].Cells[0].Value.ToString();
                txt領用部門代號.Text = Dav領用部門.Rows[r].Cells[0].Value.ToString();
                txt領用部門名稱.Text = Dav領用部門.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //this.Close();
            ResetText領用部門();
        }

        private void tab領用紀錄_SelectedIndexChanged(object sender, EventArgs e)
        {
            PublicClass.setRowNumber(Dav領用部門);
            ResetText領用部門();

            PublicClass.setRowNumber(dataGV領用_受理人);
            ResetText領用_受理人();

            PublicClass.setRowNumber(dataGV領用人);
            ResetText領用人();

            PublicClass.setRowNumber(dataGV領用);
            ResetTextLingyongjilu();
        }

        private void btnAddnew2_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "")
                    {
                        MessageBox.Show("受理人代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt領用部門代號.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_領用_受理人_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                    cmd.Parameters.AddWithValue("@受理人姓名", txt受理人姓名.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    領用_受理人load();
                    ResetText領用_受理人();
                    con.Close();
                    //reset all text
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用_受理人load();
                    return;
                }
            }
        }

        private void btnEdit2_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入受理人代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd2;
                        String sql = "SELECT COUNT(*) FROM 領用_受理人 WHERE 受理人代號=N'" + str + "'";
                        cmd2 = new SqlCommand(sql, con);
                        if ((int)cmd2.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_領用_受理人_修改";
                            cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            //con.Open();
                            cmd2.Parameters.AddWithValue("@受理人代號", str);
                            cmd2.Parameters.AddWithValue("@受理人代號2", txt受理人代號.Text);
                            cmd2.Parameters.AddWithValue("@受理人姓名", txt受理人姓名.Text);
                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            領用_受理人load();
                            con.Close();
                            ResetText領用_受理人();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt受理人代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用_受理人load();
                    return;
                }
            }
        }

        private void btnDelete2_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入受理人代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入受理人代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_領用_受理人_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            領用_受理人load();
                            con.Close();
                            ResetText領用_受理人();
                            return;
                        }
                        else
                        {
                            領用_受理人load();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    領用_受理人load();
                    return;
                }
            }
        }

        private void dataGV領用_受理人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV領用_受理人);
        }

        private void dataGV領用_受理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV領用_受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人代號.Text = dataGV領用_受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人姓名.Text = dataGV領用_受理人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnReset2_Click(object sender, EventArgs e)
        {
            ResetText領用_受理人();
        }

        private void dataGV領用人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV領用人);
        }

        private void dataGV領用人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV領用人.Rows[r].Cells[0].Value.ToString();
                txt領用人代號.Text = dataGV領用人.Rows[r].Cells[0].Value.ToString();
                txt領用部門代號2.Text = dataGV領用人.Rows[r].Cells[1].Value.ToString();
                txt領用人姓名.Text = dataGV領用人.Rows[r].Cells[2].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnReset3_Click(object sender, EventArgs e)
        {
            ResetText領用人();
        }

        private void btnAddnew3_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用人代號.Text == "")
                    {
                        MessageBox.Show("領用人代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt領用人代號.Focus();
                        return;
                    }
                    if (txt領用部門代號2.Text == "")
                    {
                        MessageBox.Show("領用部門代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt領用部門代號2.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_領用人_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@領用人代號", txt領用人代號.Text);
                    cmd.Parameters.AddWithValue("@領用部門代號", txt領用部門代號2.Text);
                    cmd.Parameters.AddWithValue("@領用人姓名", txt領用人姓名.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    領用人load();
                    ResetText領用人();
                    con.Close();
                    //reset all text
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用人load();
                    return;
                }
            }
        }

        private void btnDelete3_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用人代號.Text == "") { MessageBox.Show("請您輸入領用人代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入領用人代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_領用人_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@領用人代號", txt領用人代號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            領用人load();
                            ResetText領用人();
                            con.Close();
                            return;
                        }
                        else
                        {
                            領用人load();
                            ResetText領用人();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    領用人load();
                    ResetText領用人();
                    return;
                }
            }
        }

        private void btnEdit3_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt領用人代號.Text == "") { MessageBox.Show("請您輸入領用人代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd2;
                        String sql = "SELECT COUNT(*) FROM 領用人 WHERE 領用人代號=N'" + str + "'";
                        cmd2 = new SqlCommand(sql, con);
                        if ((int)cmd2.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_領用人_修改";
                            cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            //con.Open();
                            cmd2.Parameters.AddWithValue("@領用人代號", str);
                            cmd2.Parameters.AddWithValue("@領用人代號2", txt領用人代號.Text);
                            cmd2.Parameters.AddWithValue("@領用部門代號", txt領用部門代號2.Text);
                            cmd2.Parameters.AddWithValue("@領用人姓名", txt領用人姓名.Text);
                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            領用人load();
                            ResetText領用人();
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt領用人代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    領用人load();
                    ResetText領用人();
                    return;
                }
            }
        }

        private void btnSearch3_Click(object sender, EventArgs e)
        {
            frmSearchlingYongbumen frm = new frmSearchlingYongbumen(txt領用部門代號2);
            frm.ShowDialog();
        }

        private void txt領用部門代號2_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 領用部門 where 領用部門代號=N'{0}'", txt領用部門代號2.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName領用部門2.Text = "";
                else lbName領用部門2.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch領用_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_領用_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (txt量具編號.Text != "") cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                if (txt量具名稱.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                //if (dateTimePicker_from_歸還日期.Text != "") cmd.Parameters.AddWithValue("@歸還日期", DateTime.Parse(dateTimePicker_from_歸還日期.Text));
                //if (dateTimePicker_From_領用日期.Text != "") cmd.Parameters.AddWithValue("@領用日期", DateTime.Parse(dateTimePicker_From_領用日期.Text));                
                if (txt領用部門.Text != "") cmd.Parameters.AddWithValue("@領用人代號", txt領用部門.Text);
                if (txt受理人.Text.ToString() != "") cmd.Parameters.AddWithValue("@受理人代號", txt受理人.Text);
                cmd.Parameters.AddWithValue("@datefrom", dateTimePicker_from_歸還日期.Value);
                cmd.Parameters.AddWithValue("@todate", todate.Value);
                cmd.Parameters.AddWithValue("@datefrom2", dateTimePicker_From_領用日期.Value);
                cmd.Parameters.AddWithValue("@todate2", todate2.Value);
                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("量具編號");
                dt.Columns.Add("量具狀態");
                dt.Columns.Add("量具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("存放地點");
                dt.Columns.Add("測量範圍");
                dt.Columns.Add("數量");
                dt.Columns.Add("領用日期");
                dt.Columns.Add("歸還日期");
                dt.Columns.Add("領用人代號");
                dt.Columns.Add("受理人代號");
                dt.Columns.Add("備註");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    //dtrows["項次"]=k.ToString();
                    dtrows["量具編號"] = sqlreader["量具編號"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具名稱"] = sqlreader["量具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["存放地點"] = sqlreader["存放地點"];
                    dtrows["測量範圍"] = sqlreader["測量範圍"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["領用日期"] = sqlreader["領用日期"];
                    dtrows["歸還日期"] = sqlreader["歸還日期"];
                    dtrows["領用人代號"] = sqlreader["領用人代號"];
                    dtrows["受理人代號"] = sqlreader["受理人代號"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                    //k++;
                }
                dataGV領用.DataSource = dt;
                PublicClass.setRowNumber(dataGV領用);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn請購人員Search_Click(object sender, EventArgs e)
        {
            frmSearchlingYongbumen frm2 = new frmSearchlingYongbumen(txt領用部門);
            frm2.ShowDialog();
        }

        private void txt領用部門_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 領用部門 where 領用部門代號=N'{0}'", txt領用部門.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName領用部門.Text = "";
                else lbName領用部門.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch受理人_Click(object sender, EventArgs e)
        {
            frmSearchShouliren frm = new frmSearchShouliren(txt受理人);
            frm.ShowDialog();
        }

        private void txt受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 領用_受理人 where 受理人代號=N'{0}'", txt受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName受理人.Text = "";
                else lbName受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch量具_Click(object sender, EventArgs e)
        {
            frmSearchLiangjubianhao frm = new frmSearchLiangjubianhao(txt量具編號);
            frm.ShowDialog();
        }

        private void txt量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[2].ToString();
                if (temp == "")
                {
                    //lbName量具編號.Text = "";
                    txt量具名稱.Text = "";
                }
                else
                {
                    //lbName量具編號.Text = temp;
                    txt量具名稱.Text = temp;
                }
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt量具名稱_TextChanged(object sender, EventArgs e)
        {

        }
        string tbl0, tbl1, tbl2, tbl3, tbl4, tbl5, tbl6;
        string pt1, pt2, pt3, pt4, pt5;
        string lingyong;
        private void btnTra_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try{
                lingyong = "2";
                int r = dataGV領用.CurrentCell.RowIndex;
                string id0 = dataGV領用.Rows[r].Cells[0].Value.ToString();
                string id1 = dataGV領用.Rows[r].Cells[1].Value.ToString();
                string id6 = dataGV領用.Rows[r].Cells[2].Value.ToString();
                string id2 = dataGV領用.Rows[r].Cells[4].Value.ToString();
                string id3 = dataGV領用.Rows[r].Cells[5].Value.ToString();
                string id4 = dataGV領用.Rows[r].Cells[3].Value.ToString();
                string id5 = dataGV領用.Rows[r].Cells[6].Value.ToString();

                string id7 = dataGV領用.Rows[r].Cells[8].Value.ToString();
                string id8 = dataGV領用.Rows[r].Cells[9].Value.ToString();
                string id9 = dataGV領用.Rows[r].Cells[10].Value.ToString();
                string id10 = dataGV領用.Rows[r].Cells[11].Value.ToString();
                string id11 = dataGV領用.Rows[r].Cells[12].Value.ToString();

                tbl0 = id0;
                tbl1 = id1; //tb1 0
                tbl2 = id2; //cb1 1
                tbl3 = id3; //tb2 2
                tbl4 = id4; //tb3 3
                tbl5 = id5; //cb2 4
                tbl6 = id6;
                pt1 = id7;
                pt2 = id8;
                pt3 = id9;
                pt4 = id10;
                pt5 = id11;

                frmlingyong frm = new frmlingyong(lingyong, tbl0, tbl1, tbl2, tbl3, tbl4, tbl5, tbl6, pt1, pt2, pt3, pt4, pt5, b量具Search, b量具Insert, b量具Edit, b量具Del);
                frm.ShowDialog();
                領用紀錄load();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dataGV領用_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV領用);
        }
    }
}
